import scrapy
from itemloaders.processors import TakeFirst, MapCompose

def clean_text(text):
    """清理文本数据"""
    if text:
        return text.strip().replace('\n', '').replace('\t', '')
    return text

def convert_int(value):
    """转换为整数"""
    if value:
        if isinstance(value, str):
            # 处理"12.3万"这样的格式
            if '万' in value:
                return int(float(value.replace('万', '')) * 10000)
            # 移除逗号等字符
            value = ''.join(filter(str.isdigit, value))
        try:
            return int(value)
        except:
            return 0
    return 0

class AlbumItem(scrapy.Item):
    # 专辑信息
    album_id = scrapy.Field(output_processor=TakeFirst())
    album_title = scrapy.Field(
        input_processor=MapCompose(clean_text),
        output_processor=TakeFirst()
    )
    album_cover = scrapy.Field(output_processor=TakeFirst())
    album_description = scrapy.Field(
        input_processor=MapCompose(clean_text),
        output_processor=TakeFirst()
    )
    album_category = scrapy.Field(output_processor=TakeFirst())
    album_author = scrapy.Field(output_processor=TakeFirst())
    album_play_count = scrapy.Field(
        input_processor=MapCompose(convert_int),
        output_processor=TakeFirst()
    )
    album_track_count = scrapy.Field(
        input_processor=MapCompose(convert_int),
        output_processor=TakeFirst()
    )
    album_subscribe_count = scrapy.Field(
        input_processor=MapCompose(convert_int),
        output_processor=TakeFirst()
    )
    album_share_count = scrapy.Field(
        input_processor=MapCompose(convert_int),
        output_processor=TakeFirst()
    )
    album_url = scrapy.Field(output_processor=TakeFirst())
    created_time = scrapy.Field(output_processor=TakeFirst())

class TrackItem(scrapy.Item):
    # 音频信息
    track_id = scrapy.Field(output_processor=TakeFirst())
    track_title = scrapy.Field(
        input_processor=MapCompose(clean_text),
        output_processor=TakeFirst()
    )
    track_duration = scrapy.Field(output_processor=TakeFirst())
    track_play_count = scrapy.Field(
        input_processor=MapCompose(convert_int),
        output_processor=TakeFirst()
    )
    track_like_count = scrapy.Field(
        input_processor=MapCompose(convert_int),
        output_processor=TakeFirst()
    )
    track_comment_count = scrapy.Field(
        input_processor=MapCompose(convert_int),
        output_processor=TakeFirst()
    )
    track_cover = scrapy.Field(output_processor=TakeFirst())
    track_mp3_url = scrapy.Field(output_processor=TakeFirst())
    album_id = scrapy.Field(output_processor=TakeFirst())
    created_time = scrapy.Field(output_processor=TakeFirst())